package cn.tedu.lonnghospital.appointment.controller;

import cn.tedu.lonnghospital.appointment.pojo.dto.AppointmentAddNewDTO;
import cn.tedu.lonnghospital.appointment.pojo.dto.AppointmentUpdateDTO;
import cn.tedu.lonnghospital.appointment.pojo.vo.AppointmentListItemVO;
import cn.tedu.lonnghospital.appointment.pojo.vo.AppointmentStandardVO;
import cn.tedu.lonnghospital.appointment.pojo.vo.PageData;
import cn.tedu.lonnghospital.appointment.service.AppointmentService;
import cn.tedu.lonnghospital.appointment.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


@RestController
@Api(tags = "预约管理面板")
@RequestMapping("/appointment")
@Validated
@Slf4j
public class AppointmentController {
    @Autowired
    AppointmentService appointmentService;

    @ApiOperationSupport(order = 100)
    @ApiOperation("添加预约")
    @PostMapping("/addNew")
    public JsonResult addNew(@Valid AppointmentAddNewDTO appointmentAddNewDTO) {
        log.debug("开始处理【添加预约】请求，参数：{}", appointmentAddNewDTO);
        appointmentService.addNew(appointmentAddNewDTO);
        log.debug("【添加预约】请求处理成功！");
        return JsonResult.ok();
    }

    @ApiOperationSupport(order = 200)
    @ApiOperation("取消预约")
    @PostMapping("/deleteById")
    @ApiImplicitParam(name = "id", value = "预约ID", dataType = "long", required = true)
    public JsonResult deleteById(@Range(min = 1, message = "取消预约失败！预约ID必须为正整数！")
                                 @RequestParam long id) {
        log.debug("开始处理【取消预约】请求，参数：{}", id);
        appointmentService.deleteById(id);
        log.debug("【取消预约】请求处理成功！");
        return JsonResult.ok();
    }

    @ApiOperationSupport(order = 300)
    @ApiOperation("根据预约id修改预约信息")
    @GetMapping("/updateById")
    @ApiImplicitParam(name = "id", value = "预约ID", dataType = "long", required = true)
    public JsonResult updateById(@Range(min = 1, message = "修改失败！预约ID必须为正整数！")
                                      @RequestParam long id, @Valid AppointmentUpdateDTO appointmentUpdateDTO) {
        log.debug("开始处理【根据预约id修改预约信息】请求，参数：{},{}", id,appointmentUpdateDTO);
        appointmentService.updateById(id,appointmentUpdateDTO);
        log.debug("【根据预约id修改预约信息】请求处理成功！");
        return JsonResult.ok();
    }

    @ApiOperationSupport(order = 310)
    @ApiOperation("根据预约id完成就诊")
    @GetMapping("/updateFinishById")
    @ApiImplicitParam(name = "id", value = "预约ID", dataType = "long", required = true)
    public JsonResult updateFinishById(@Range(min = 1, message = "完成就诊失败！预约ID必须为正整数！")
                                 @RequestParam long id) {
        log.debug("开始处理【根据预约id完成就诊】请求，参数：{}", id);
        appointmentService.updateFinishById(id);
        log.debug("【根据预约id完成就诊】请求处理成功！");
        return JsonResult.ok();
    }

    @ApiOperationSupport(order = 400)
    @ApiOperation("根据患者id查询预约列表")
    @GetMapping("/listByPatientId")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "id", value = "患者ID", dataType = "long", required = true),
                    @ApiImplicitParam(name = "pageNum", value = "页码", dataType = "Integer", required = true)
            }
    )

    public JsonResult listByPatientId(@Range(min = 1, message = "查询失败！患者ID必须为正整数！")
                                      @RequestParam long id, Integer pageNum) {
        if(pageNum==null){
            pageNum=1;
        }
        log.debug("开始处理【根据患者id查询预约列表】请求，参数：{},{}", id,pageNum);
        PageData<AppointmentListItemVO> pageData=appointmentService.listByPatientId(id,pageNum);
        log.debug("【根据患者id查询预约列表】请求处理成功！");
        return JsonResult.ok(pageData);
    }

    @ApiOperationSupport(order = 410)
    @ApiOperation("根据医生id查询预约列表")
    @GetMapping("/listByDoctorId")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "id", value = "医生ID", dataType = "long", required = true),
                    @ApiImplicitParam(name = "pageNum", value = "页码", dataType = "Integer", required = true)
            }
    )

    public JsonResult listByDoctorId(@Range(min = 1, message = "查询失败！医生ID必须为正整数！")
                                      @RequestParam long id, Integer pageNum) {
        if(pageNum==null){
            pageNum=1;
        }
        log.debug("开始处理【根据医生id查询预约列表】请求，参数：{},{}", id,pageNum);
        PageData<AppointmentListItemVO> pageData=appointmentService.listByDoctorId(id,pageNum);
        log.debug("【根据医生id查询预约列表】请求处理成功！");
        return JsonResult.ok(pageData);
    }

    @ApiOperationSupport(order = 420)
    @ApiOperation("根据预约id查询预约信息")
    @GetMapping("/getStandard")
    @ApiImplicitParam(name = "id", value = "预约ID", dataType = "long", required = true)
    public JsonResult listByPatientId(@Range(min = 1, message = "查询失败！预约ID必须为正整数！")
                                      @RequestParam long id) {
        log.debug("开始处理【根据预约id查询预约信息】请求，参数：{}", id);
        AppointmentStandardVO appointmentStandardVO= appointmentService.getStandard(id);
        log.debug("【根据预约id查询预约信息】请求处理成功！");
        return JsonResult.ok(appointmentStandardVO);
    }

}
